﻿using pcObjectLibrary;
using pcBLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PunchClock.Models;
using System.Web.Security;
using Microsoft.Web.WebPages.OAuth;
using System.Collections.ObjectModel;
using pcCore;

namespace PunchClock.UI.Web.Controllers
{
    public class UserController : BaseController
    {
        //
        // GET: /Register/

        public ActionResult Register()
        {
            if (User.Identity.IsAuthenticated)
                return RedirectToAction("Edit", "User", new { userName = operatingUser.userName });
            UserObjLibrary user = new UserObjLibrary();
            user.lastActivity_ip = userSession.ipAddress;
            user.lastActive_MAC_address = userSession.MacAddress;

            ReadOnlyCollection<TimeZoneInfo> tz;
            tz = TimeZoneInfo.GetSystemTimeZones();
            user.timezonesList = (from t in tz
                                  orderby t.Id
                                  select new SelectListItem
                                  {
                                       Value = t.Id,
                                       Text = t.Id
                                  }).ToList();
            user.timezonesList.Where(x => x.Value == "US Eastern Standard Time").Single().Selected = true;

            var UserTypes = Common.GetUserTypes(adminCall: true);
            if (user.userTypeId > 0)
                UserTypes.Where(x => x.Value == user.userTypeId.ToString()).First().Selected = true;
            ViewBag.UserTypes = UserTypes;

            return View(user);
        }

        [HttpPost]
        public JsonResult Register(UserObjLibrary user, FormCollection coll)
        {
            user.userRegistered_ip = userSession.ipAddress;
            user.registered_MAC_address = userSession.MacAddress;
            user.lastActivity_ip = userSession.ipAddress;
            user.lastActive_MAC_address = userSession.MacAddress;
            user.EmploymentTypeId = (int)EmploymentTypeEnum.ContractHourly; // this is default employemnt type at registration. later admin can set the type
            UserBLL ub = new UserBLL();
            user.userId = ub.Add(user);
            return Json(new { user = user });
        }

        [HttpGet]
        [Authorize]
        public ActionResult Edit(string userName)
        {
            UserObjLibrary user = new UserObjLibrary();
            UserBLL ub = new UserBLL();
            user = ub.Details(userName);
            user.userRegistered_ip = userSession.ipAddress;
            user.registered_MAC_address = userSession.MacAddress;
            user.lastActivity_ip = userSession.ipAddress;
            user.lastActive_MAC_address = userSession.MacAddress;
            return View(user);
        }

        [HttpPost]
        [Authorize]
        public JsonResult Edit(UserObjLibrary user)
        {
            user.userRegistered_ip = userSession.ipAddress;
            user.registered_MAC_address = userSession.MacAddress;
            user.lastActivity_ip = userSession.ipAddress;
            user.lastActive_MAC_address = userSession.MacAddress;
            UserBLL ub = new UserBLL();
            user.userId = ub.Update(user, false);
            return Json(new { user = user });
        }
        [HttpGet]
        [Authorize]
        public ActionResult Details(int id)
        {
            UserObjLibrary user = new UserObjLibrary();
            UserBLL ub = new UserBLL();
            user = ub.Details(userId: id);

            ReadOnlyCollection<TimeZoneInfo> tz;
            tz = TimeZoneInfo.GetSystemTimeZones();
            user.timezonesList = (from t in tz
                                  orderby t.Id
                                  select new SelectListItem
                                  {
                                      Value = t.Id,
                                      Text = t.Id
                                  }).ToList();
            user.timezonesList.Where(x => x.Value == user.registeredTimeZone).Single().Selected = true;
            var EmploymentTypes = Common.GetEmploymentTypes();
            EmploymentTypes.Where(x => x.Value == user.EmploymentTypeId.ToString()).First().Selected = true;
            ViewBag.EmploymentType = EmploymentTypes;

            var UserTypes = Common.GetUserTypes(adminCall: true);
            UserTypes.Where(x => x.Value == user.userTypeId.ToString()).First().Selected = true;
            ViewBag.UserTypes = UserTypes;

            if (user.lastPunch.punchIn != null && user.lastPunch.punchIn != TimeSpan.MinValue)
            {
                user.lastPunch.punchIn = 
                    TimeZoneInfo.ConvertTimeFromUtc( user.lastPunch.punchDate.Date +  user.lastPunch.punchIn, TimeZoneInfo.FindSystemTimeZoneById(operatingUser.registeredTimeZone)).TimeOfDay;
            }
            if (user.lastPunch.punchOut != null && user.lastPunch.punchOut != TimeSpan.MinValue)
            {
                user.lastPunch.punchOut = user.lastPunch.punchOut.HasValue ? 
                    TimeZoneInfo.ConvertTimeFromUtc(user.lastPunch.punchDate.Date + user.lastPunch.punchOut.Value, 
                    TimeZoneInfo.FindSystemTimeZoneById(operatingUser.registeredTimeZone)).TimeOfDay : TimeSpan.MinValue;
            }


            return PartialView("_Details", user);
        }

        [HttpPost]
        [Authorize]
        public ActionResult Details(UserObjLibrary obj, bool adminUpdate = false)
        {
            UserObjLibrary user = new UserObjLibrary();
            UserBLL ub = new UserBLL();
            user.userId = ub.Update(obj, adminUpdate);
            return Json(new { user = obj });
        }
    }
}
